<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use \DB;
use App\Models\ProjectReview;
use App\Models\Project;
use App\Http\Requests\ProjectAuditRequest;

class AllProjectController extends CommonController
{
    /**
     * 获取完整度大于85%的待评审的项目列表
     */
    public function getList()
    {
        $lists = Project::with('user', 'zone', 'admin')->where('project.progress', '>', '85')->orderBy('created_at', 'desc')
            ->orderBy('review_admin_id', 'asc')->paginate(10);

        $admins = Admin::get();

        return view('allproject.list')->with("lists", $lists)->with("admins", $admins)->with("actions", $this->actions);
    }

    /**
     * 获取完整度大于85%并且符合搜索条件的项目列表
     */
    public function search(Request $request)
    {
        $data = $request->input();
//        dd($data);exit;

        $lists = Project::with('user', 'zone', 'admin')->where('project.progress', '>', '85')
            ->orderBy('review_admin_id', 'asc');

        if (isset($data['subject']) && $data['subject'] != '') {
            $lists->where('subject', 'like', '%' . $data['subject'] . '%');
        }

        if (isset($data['audit']) && $data['audit'] == 0 ) {
            $lists->where('integrity', '=', 0);
        } elseif (isset($data['audit']) && $data['audit'] == 1) {
            $lists->where('integrity', '>=', 20);
        }

        if (isset($data['admin']) && $data['admin'] != '') {
            $lists->where('review_admin_id', '=', $data['admin']);
        }

//        dd($lists->toSql());exit;

        $lists = $lists->paginate(10);
        $admins = Admin::get();

        return view('allproject.search')->with("lists", $lists)->with("admins", $admins)->with("subject", $data['subject'])
            ->with("audit", $data['audit'])->with("admin", $data['admin'])->with("actions", $this->actions);
    }

    /**
     * 批量分配项目到管理员名下
     */
    public function postAssign(Request $request)
    {
        $this->validate($request, ['admin' => 'required|numeric']);

        $data = $request->input();
        $projects = $this->explode($data['project']);

        $result = 0;
        foreach ($projects as $v) {
            Project::where('id', $v)->update(['review_admin_id' => $data['admin']]);
            $result++;
        }

        $adminUser = Admin::where('id', $data['admin'])->first();

        if ($result) {
            return AjaxCallbackMessage('成功分配' . $result . '条任务给' . $adminUser->nickname, true, url('allproject/list'));
        } else {
            return AjaxCallbackMessage('任务分配失败', false);
        }
    }

    /**
     * 获取登录用户的项目评审列表
     */
    public function getMyList()
    {
        $session = session()->all();
        $id = $session['user_role']->id;

        $lists = Project::with('user', 'zone', 'admin')->where('project.progress', '>', '85')
            ->where('project.review_admin_id', '=', $id)->orderBy('project.id', 'asc')->paginate(10);

        return view('allproject.mylist')->with("lists", $lists)->with("actions", $this->actions);
    }

    /**
     * 获取评审界面
     */
    public function getAudit(Request $request)
    {
        $id = $request->input('id');
        $session = session()->all();
        $name = $session['user_role']->name;
        $project = Project::find($id)->toArray();
        $data = [
            'id'       => $project['id'],
            'subject'  => $project['subject'],
            'progress' => $project['progress'],
            'name'     => $name,
        ];

        return view('allproject.audit')->with('data', $data);
    }

    /**
     * 评审一个项目
     */
    public function postAudit(ProjectAuditRequest $request)
    {
        $data = $request->input();

        unset($data['_token']);
        $total = $data['integrity'] + $data['industry'] + $data['team'] + $data['profit'] + $data['wind_control'];
        $data['total'] = $total;

        $data['level'] = $this->level($total);

        $review = ProjectReview::where('project_id', $data['project_id'])->get();

        if (is_object($review)) {
            ProjectReview::where('project_id', $data['project_id'])
                ->update($data);
            Project::where('id', $data['project_id'])->update(['integrity' => $data['industry']]);

            return AjaxCallbackMessage('再次评审成功，该项目等级为' . $data['level'], true, url('allproject/my-list'));
        }

        Project::where('id', $data['project_id'])->update(['integrity' => $data['industry']]);
        $result = ProjectReview::create($data);

        if (is_object($result)) {
            return AjaxCallbackMessage('评审成功，该项目等级为' . $data['level'], true, url('allproject/my-list'));
        } else {
            return AjaxCallbackMessage('评审失败', false);
        }
    }

    private function level($total)
    {
        if ($total >= 85) {
            $level = 'A';
        } elseif ($total < 85 && $total >= 75) {
            $level = 'B';
        } elseif ($total < 75 && $total >= 60) {
            $level = 'C';
        } else {
            $level = 'D';
        }

        return $level;
    }

    private function explode($data)
    {
        $projectStr = explode(",", $data);
        $projects = [];
        foreach ($projectStr as $v) {
            if (is_numeric($v)) {
                $projects[] = $v;
            }
        }

        return $projects;
    }
}
